package GUI;

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import logic.Book;
import logic.Loan;
import logic.Member;
import logic.Message;

import javax.swing.JTextPane;

import client.OBL_Client;

import java.awt.ScrollPane;
import javax.swing.JCheckBox;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.UIManager;
import javax.swing.JTextField;

import server.MessageController;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class ViewLoan extends JPanel{
	
	private Client_GUI parent;
	private JLabel loanTitle;
	private JLabel lidLBL;
	private JLabel uidLBL;
	private JLabel bidLBL;
	private JLabel bcidLBL;
	private JLabel loandateLBL;
	private Loan loan;
	
	private ResultDisplay results;
	private JLabel lblHotBook;
	private JCheckBox extendedChkBox;
	private JLabel returndate;
	private JLabel status;
	private JLabel statusLBL;
	private JLabel returndateLBL;
	private JCheckBox chckbxHotBook;
	private JTextField daystxtField;
		
	private JLabel lblExtendDays;
	private JButton btnExtendLoan;
	private JLabel LBL_Days;
	private JLabel lblBookName;
	private JLabel lblMemberName;
	private JLabel bNameLBL;
	private JLabel mNameLBL;
	
	public ViewLoan(final Client_GUI parent,final Loan loan,final ResultDisplay results) {
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		this.setBackground(UIManager.getColor("inactiveCaption"));

		this.loan = loan;
		this.results = results;
		
		
		loanTitle = new JLabel("Search Book");
		loanTitle.setForeground(Color.RED);
		loanTitle.setFont(new Font("Tahoma", Font.BOLD, 22));
		loanTitle.setBounds(290, 62, 227, 43);
		add(loanTitle);
		
		JLabel lid = new JLabel("Loan ID:");
		lid.setFont(new Font("Tahoma", Font.BOLD, 12));
		lid.setBounds(160, 187, 113, 14);
		add(lid);
		
		JLabel bid = new JLabel("Book ID:");
		bid.setFont(new Font("Tahoma", Font.BOLD, 12));
		bid.setBounds(160, 238, 131, 14);
		add(bid);
		
		JLabel uid = new JLabel("Member ID:");
		uid.setFont(new Font("Tahoma", Font.BOLD, 12));
		uid.setBounds(160, 213, 131, 14);
		add(uid);
		
		JLabel bcid = new JLabel("Book Copy ID:");
		bcid.setFont(new Font("Tahoma", Font.BOLD, 12));
		bcid.setBounds(160, 263, 131, 14);
		add(bcid);
		
		JLabel extended = new JLabel("Extended:");
		extended.setFont(new Font("Tahoma", Font.BOLD, 12));
		extended.setBounds(478, 187, 131, 14);
		add(extended);
		
		JLabel loandate = new JLabel("Loan Date:");
		loandate.setFont(new Font("Tahoma", Font.BOLD, 12));
		loandate.setBounds(160, 288, 131, 14);
		add(loandate);
		
		JButton btnCancel = new JButton("Back");
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				parent.RepaintMain(results);
				
			}
		});
		btnCancel.setBounds(450, 413, 160, 34);
		add(btnCancel);
		
		lidLBL = new JLabel("");
		lidLBL.setBounds(283, 187, 88, 14);
		add(lidLBL);
		
		uidLBL = new JLabel("");
		uidLBL.setBounds(283, 213, 88, 14);
		add(uidLBL);
		
		bidLBL = new JLabel("");
		bidLBL.setBounds(283, 238, 88, 14);
		add(bidLBL);
		
		bcidLBL = new JLabel("");
		bcidLBL.setBounds(283, 263, 88, 14);
		add(bcidLBL);
		
		loandateLBL = new JLabel("");
		loandateLBL.setBounds(283, 288, 88, 14);
		add(loandateLBL);
		
		lblHotBook = new JLabel("Hot Book:");
		lblHotBook.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblHotBook.setBounds(478, 213, 131, 14);
		add(lblHotBook);
		
		extendedChkBox = new JCheckBox("Extended");
		extendedChkBox.setEnabled(false);
		extendedChkBox.setBounds(596, 184, 97, 23);
		add(extendedChkBox);
		
		returndate = new JLabel("Return Date:");
		returndate.setFont(new Font("Tahoma", Font.BOLD, 12));
		returndate.setBounds(160, 313, 131, 14);
		add(returndate);
		
		status = new JLabel("Status:");
		status.setFont(new Font("Tahoma", Font.BOLD, 12));
		status.setBounds(478, 238, 97, 14);
		add(status);
		
		statusLBL = new JLabel((String) null);
		statusLBL.setBounds(596, 239, 88, 14);
		add(statusLBL);
		
		LBL_Days = new JLabel("");
		LBL_Days.setBounds(337, 383, 20, 20);
		add(LBL_Days);
		
		returndateLBL = new JLabel((String) null);
		returndateLBL.setBounds(283, 313, 88, 14);
		add(returndateLBL);
		
		chckbxHotBook = new JCheckBox("Hot Book");
		chckbxHotBook.setEnabled(false);
		chckbxHotBook.setBounds(596, 210, 97, 23);
		add(chckbxHotBook);
		
		lblExtendDays = new JLabel("Extend Days:");
		lblExtendDays.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblExtendDays.setBounds(160, 384, 131, 14);
		add(lblExtendDays);
		
		btnExtendLoan = new JButton("Extend Loan");
		btnExtendLoan.setEnabled(false);
		btnExtendLoan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Validate(true);
			}
		});
		btnExtendLoan.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnExtendLoan.setBounds(160, 413, 172, 34);
		add(btnExtendLoan);
		
		daystxtField = new JTextField();
		daystxtField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				Validate(false);
			}
		});
		daystxtField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				Validate(false);
			}
		});
		daystxtField.setBounds(270, 382, 62, 20);
		add(daystxtField);
		daystxtField.setColumns(10);
		
		lblBookName = new JLabel("Book Name:");
		lblBookName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblBookName.setBounds(160, 105, 113, 14);
		add(lblBookName);
		
		lblMemberName = new JLabel("Member Name:");
		lblMemberName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMemberName.setBounds(160, 130, 113, 14);
		add(lblMemberName);
		
		bNameLBL = new JLabel("0");
		bNameLBL.setBounds(269, 106, 145, 14);
		add(bNameLBL);
		
		mNameLBL = new JLabel("0");
		mNameLBL.setBounds(270, 130, 145, 14);
		add(mNameLBL);
		mNameLBL.setVisible(true);
		bNameLBL.setVisible(true);
		lblMemberName.setVisible(true);
		lblBookName.setVisible(true);
		mNameLBL.setText(loan.getmemberName());
		bNameLBL.setText(loan.getbookName());
		
		switch(parent.client.Privelege){
		
		case Member.LIBRARIAN_PRIVILIGE:{
			lblExtendDays.setVisible(true);
			daystxtField.setVisible(true);
			btnExtendLoan.setVisible(true);
			break;


		}
		case Member.MEMBER_PRIVILIGE:{
			
			mNameLBL.setVisible(false);
			lblMemberName.setVisible(false);
			
			
			lblExtendDays.setVisible(false);
			daystxtField.setVisible(false);
			btnExtendLoan.setVisible(true);	
			
			if((loan.getHotBook() == 0) && (loan.getextended() == 0)){
				
				
				
				String retDate = loan.getActualReturnDate();
				
				int year = Integer.parseInt(retDate.substring(0, 4));
				int month = Integer.parseInt(retDate.substring(5, 7));
				int day = Integer.parseInt(retDate.substring(8, retDate.length()));

				Calendar validator = Calendar.getInstance();
				validator.set(year, month-1, day);
				validator.setLenient(false);
				validator.getTime();
				
				
				Calendar currentDate = Calendar.getInstance();
				currentDate.add(Calendar.DAY_OF_MONTH, 7);
				
				if ((currentDate.getTimeInMillis() - validator.getTimeInMillis()) > 0) btnExtendLoan.setEnabled(true);
			}
			
			break;
		}
		
		case Member.MANAGER_PRIVILIGE:{
			lblExtendDays.setVisible(false);
			daystxtField.setVisible(false);
			btnExtendLoan.setVisible(false);			
			break;
		}
		
		}

		InitBookData();
		
	}
	
	private void InitBookData(){
		loanTitle.setText("Loan Information");
		lidLBL.setText(String.valueOf(loan.getLID()));
		uidLBL.setText(String.valueOf(loan.getUID()));
		bidLBL.setText(String.valueOf(loan.getBID()));
		bcidLBL.setText(String.valueOf(loan.getBCID()));
		returndateLBL.setText(loan.getActualReturnDate());
		loandateLBL.setText(loan.getLoanDate());
		statusLBL.setText(String.valueOf(loan.getstatus()));
		if (loan.getHotBook() == 1) chckbxHotBook.setSelected(true);
		if (loan.getextended() == 1) extendedChkBox.setSelected(true);
	}
	
	
	private void Validate(boolean fromOk){
		boolean valid = true;
		ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
		ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		try{
			String tmep = daystxtField.getText();
			if(!tmep.equals("")){
				char c = tmep.charAt(0);
				if ((c == '+') || (c== '-')){
					valid = false;
					LBL_Days.setIcon(BAD);
				}else{
					int days = Integer.parseInt(tmep);
					LBL_Days.setIcon(OK);
				}	
			}
		}catch(NumberFormatException nu){
			LBL_Days.setIcon(BAD);
			valid = false;
		}
		
		if(valid){
			btnExtendLoan.setEnabled(true);
		}
		
		if (fromOk && valid){
			
			
			ArrayList<String> indexes = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
			int action = Message.EXTEND_LOAN_REQUEST;
			
			indexes.add("LID");
			data.add(loan.getLID());
			
			indexes.add("TYPE");
			switch(parent.client.Privelege){
			
				case Member.LIBRARIAN_PRIVILIGE:{
					data.add("MANUAL");
					
					indexes.add("DAYS");
					data.add(daystxtField.getText());
					break;
				}
				
				case Member.MEMBER_PRIVILIGE:{
					data.add("AUTOMATIC");
					break;
				}
			
			}
			
			Message toSend = new Message(indexes,data,action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
								
			}else{//message send failed
			}			
		}
	}
}
